package com.cyufeng.blog.repository;

import com.cyufeng.blog.entity.Article;
import com.cyufeng.blog.entity.Sensitive;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;


import java.sql.Date;
import java.util.Set;

/**
 * 敏感词
 * @author Cyufeng
 * Data:2019.03.21
 */
@Repository
public interface SensitiveRepository extends JpaRepository<Sensitive,Integer> {
    /**
     * 得到所有敏感词
     *
     * @return
     */
    @Query(value = "select t.WORD from word t where t.DR ='0' and t.IS_ENABLE = '1'", nativeQuery = true)
    Set<String> getAllWord();

//    /**
//     * 有条件得到所有敏感词，并且分页--用于后台操作
//     *
//     * @param pageable
//     * @return
//     */
//    @Query(value = "select * from word t where t.DR ='0'  and t.CLASS = ? ORDER BY MADE_TIME DESC", countQuery = "select count(*) from word t where t.DR ='0' and t.CLASS = ? ORDER BY MADE_TIME DESC", nativeQuery = true)
//    Page<Sensitive> getAllSensitive_page(int sort, Pageable pageable);

    /**
     * 得到所有敏感词，并且分页--用于后台操作
     *
     * @param pageable
     * @return
     */
    @Query(value = "select * from word t where t.DR ='0' ORDER BY MADE_TIME DESC", countQuery = "select count(*) from word t where t.DR ='0'  ORDER BY MADE_TIME DESC", nativeQuery = true)
    Page<Sensitive> getAllSensitive_page(Pageable pageable);

    /**
     * 更新是否启用字段
     *
     * @return
     */
    @Modifying
    @Query(value = "update word t set IS_ENABLE = ? where t.DR ='0' and t.id =?", nativeQuery = true)
    void setIsEnable(int isEnable, int id);

    /**
     * 保存修改操作
     *
     * @return
     */
    @Modifying
    @Query(value = "update word t set WORD = ? where t.DR ='0' and t.id =?", nativeQuery = true)
    void saveEdit(String newWord, int id);

    /**
     * 删除敏感词
     *
     * @return
     */
    @Modifying
    @Query(value = "update word t set t.DR = 1 , t.DEL_TIME = ? where t.id =?", nativeQuery = true)
    void deleteWord(Date date, int id);

}
